/*package dao.hibernate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.mokai.SSH.domain.Page;

@Transactional//让本类中的方法也开启事务 
public abstract class DaoSupport<T> implements Dao<T> {

	@Resource
	protected SessionFactory factory;
	protected Class<T> entityClass;//实体类class
	protected  String entityName;//实体类名
	
	public DaoSupport(){
		entityClass=getEntityClass();
		entityName=getEntityName();
	}
	
	private Session getSession(){
		return this.factory.getCurrentSession();
	}
	
	*//**
	 * 获得实体类型
	 * @return
	 *//*
	@SuppressWarnings("unchecked")
	protected Class<T> getEntityClass(){
		//获得泛型的类型
		Type type=getClass().getGenericSuperclass();
		if(type instanceof ParameterizedType){
			ParameterizedType ptype=(ParameterizedType)type;
			return (Class<T>)ptype.getActualTypeArguments()[0];
		}
		return null;
	}
	*//**
	 * 获得实体名称
	 * @return
	 *//*
	protected String getEntityName(){
		return entityClass.getSimpleName();
	}
	
	
	
	*//****
	 * 根据Id删除
	 *//*
	@Override
	public void delete(Serializable... entityId){
		StringBuilder sb=new StringBuilder("delete from ");
		sb.append(this.entityName).append(" where id in(");
		for (int i = 0; i < entityId.length; i++) {
			if(i!=0)sb.append(",");
			sb.append(entityId[i]);
		}
		sb.append(")");
		this.execute(sb.toString());
	}
	
	@Override
	public  T get(Serializable entityId){
		return (T) this.getSession().get(entityClass, entityId);
	}

	@Override
	public  void add(T entity){
		this.getSession().persist(entity);
	}

	@Override
	public  void update(T entity){
		this.getSession().merge(entity);
	}
	
	
	
	@Override
	public  Long getCount(){
		String sql="select count(o) from "+this.entityName+" as  o";
		return (Long)this.getSession().createQuery(sql).uniqueResult();
	}
	
	*//***
	 * 根据hql来查找
	 * @param hql
	 * @param param 参数
	 * @return	返回一个List的结果集
	 *//*
	@SuppressWarnings("unchecked")
	public List<T> findByHql(String hql,Object... param){
		Query query=this.getSession().createQuery(hql);
		for (int i = 0; param!=null && i < param.length; i++) {
			query.setParameter(i+1, param[i]);
		}
		return query.list();
	}
	
	
	@Override
	public T getByHql(String hql, Object... param) {
		List<T> result=findByHql(hql, param);
		if(result!=null && result.size()>0){
			return result.get(0);
		}
		return null;
	}
	
	
	@Override
	public Object getFirstResult(String hql, Object... param) {
		Query query=this.getSession().createQuery(hql);
		for (int i = 0; param!=null && i < param.length; i++) {
			query.setParameter(i+1, param[i]);
		}
		return query.uniqueResult();
	}
	
	*//***
	 * 获得所有数据
	 * @return 
	 *//*
	@Override
	public List<T> findAll(){
		String hql="from "+this.entityName;
		return this.findByHql(hql, null);
	}
	
	*//***
	 * 分页查询
	 * @param page 分页bean
	 * @return 指定页得数据
	 *//*
	@SuppressWarnings("unchecked")
	public Page<T> findByPage(Page<T> page){
		//总记录数
		String hql="select count(o) from "+this.entityName+" as o ";
		if(!"".equals(page.getWhereSql()) && page.getWhereSql()!=null){
			hql+=" where "+page.getWhereSql();
		}
		page.setTotalRow((Long)getFirstResult(hql,page.getWhereParameter()));
		//构建查询hql语句
		hql=buildHql(page);
		Query query=this.getSession().createQuery(hql);
		query.setFirstResult((page.getCurrentPage()-1)*page.getSize()).setMaxResults(page.getCurrentPage()*page.getSize());
		Object[] paramter=page.getWhereParameter();
		for (int i = 0; paramter!=null && i < paramter.length; i++) {
			query.setParameter(i+1, paramter[i]);
		}
		page.setResult(query.list());
		return page;
	}
	
	*//***
	 * 构建hql语句
	 * @param page	
	 * @return	
	 *//*
	private String buildHql(Page<T> page){
		StringBuilder sb=new StringBuilder("from "+this.entityName+" as o");
		if(page.getWhereSql()!=null && !"".equals(page.getWhereSql())){
			sb.append(" where ").append(page.getWhereSql());
		}
		if(page.getOrderItems()!=null && page.getOrderItems().size()>0){
			sb.append(" order by ");
			for (Entry<String, String> entry : page.getOrderItems().entrySet()) {
				sb.append("o.").append(entry.getKey()).append(" ").append(entry.getValue());
			}
		}
		return sb.toString();
	}
	
	*//**
	 * 执行语句
	 *//*
	@Override
	public void execute(String hql, Object... param) {
		Query query=this.getSession().createQuery(hql);
		for (int i = 0; param!=null &&  i < param.length; i++) {
			query.setParameter(i+1, param[i]);
		}
		query.executeUpdate();
	}
	
	
	*//***
	 * 更新表数据
	 * @param datas 字段名与数据的键值对
	 * @param whereSql where条件，不带where,且占位的索引应接着datas的个数一直累加，请切记
	 * @param whereParams 与where条件相对应的值
	 *//*
	@Override
	public void updateByHql(Map<String, Object> datas,String whereSql,Object... whereParams) {
		StringBuilder sb=new StringBuilder("update ");
		sb.append(this.entityName).append(" set ");
		List<Object> params=new ArrayList<Object>();
		for (Entry<String, Object> entry : datas.entrySet()) {
			if(params.size()>0) sb.append(",");
			params.add(entry.getValue());
			sb.append(entry.getKey()).append("=?");
		}
		if(whereSql!=null && !"".equals(whereSql)){
			sb.append(" where ").append(whereSql);
			//把where参数附加到后面
			params.addAll(Arrays.asList(whereParams));
			this.execute(sb.toString(), params.toArray());
		}else{
			this.execute(sb.toString(), params.toArray());
		}
	}
	
	*//****
	 * 根据id来更新表数据
	 * @param datas
	 * @param id
	 *//*
	@Override
	public void updateById(Map<String, Object> datas,Serializable id){
		String whereSql=" id=?";
		this.updateByHql(datas, whereSql, id);
	}

}
*/